package com.classeven.downhill.engine;

import org.andengine.audio.sound.Sound;
import org.andengine.entity.modifier.MoveYModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ScrollDetector;
import org.andengine.input.touch.detector.ScrollDetector.IScrollDetectorListener;
import org.andengine.ui.activity.SimpleBaseGameActivity;

public class Menu_Scroll_Y implements IScrollDetectorListener
{
	private int				RES_WIDTH;
	private int				RES_HEIGHT;
	
	private int				y_Start;
	private int				y_Min;
	private int				y_Max;
	private int[]			y_Center;
	
	private int				m_Focus				= 0;
	
	private int				DELTA				= 5;
	private float			DURATION			= 0.3f;
	
	private ScrollDetector	m_ScrollDetector	= new ScrollDetector(this);
	private Sprite[]		m_Sprites;
	private Sound			sound;
	
		
	public Menu_Scroll_Y(SimpleBaseGameActivity p_Activity,  float CenterY, Sprite... p_Sprites)
	{
		
//		this.sound = sound;
//		sound.setVolume(0.5f);
		
		RES_WIDTH = p_Activity.getWindowManager().getDefaultDisplay()
				.getWidth();
		RES_HEIGHT = p_Activity.getWindowManager().getDefaultDisplay()
				.getHeight();
		
		y_Start = (int) (((RES_HEIGHT - p_Sprites[0].getHeight()) / 2) + CenterY);
		y_Min = (int) (y_Start - DELTA / 2);
		y_Max = (int) (y_Min + p_Sprites[0].getHeight() + DELTA);
		
		y_Center = new int[p_Sprites.length];
		m_Sprites = new Sprite[p_Sprites.length];
		
		for (int i = 0; i < p_Sprites.length; i++)
		{
			m_Sprites[i] = p_Sprites[i];
			m_Sprites[i].setPosition( m_Sprites[i].getX(),
					(y_Start + ((m_Sprites[i].getHeight() + DELTA) * i)));
			
			if (i != 0)
				m_Sprites[i].setScale(0.75f);
		}
	}
	
	public void onTouchEvent(TouchEvent pSceneTouchEvent)
	{
		m_ScrollDetector.onTouchEvent(pSceneTouchEvent);
	}
	
	
	public void onScrollStarted(ScrollDetector pScollDetector, int pPointerID,
			float pDistanceX, float pDistanceY)
	{}
	
	
	public void onScroll(ScrollDetector pScollDetector, int pPointerID,
			float pDistanceX, float pDistanceY)
	{
//		sound.play();
		
		
		if (m_Sprites[0].getY() >= y_Start && pDistanceY >= 0)
			for (int i = 0; i < m_Sprites.length; i++)
			{
				m_Sprites[i].setPosition(m_Sprites[i].getX(),
						(y_Start + ((m_Sprites[i].getHeight() + DELTA) * i)));
			}
		else if (m_Sprites[m_Sprites.length - 1].getY() <= y_Start
				&& pDistanceY <= 0)
			for (int i = 0; i < m_Sprites.length; i++)
			{
				m_Sprites[i].setPosition(m_Sprites[i].getX(),
						(y_Start + ((m_Sprites[i].getHeight() + DELTA) * (i + 1 - m_Sprites.length))));
						
			}
		else
			for (int i = 0; i < m_Sprites.length; i++)
			{
				m_Sprites[i].setPosition(m_Sprites[i].getX() ,
						m_Sprites[i].getY() + pDistanceY / 2);
				
				y_Center[i] = (int) (m_Sprites[i].getY() + m_Sprites[i]
						.getHeight() / 2);
				
				// scale tiap kali di scroll
				if (y_Center[i] >= y_Min && y_Center[i] <= y_Max)
				{
					if (y_Center[i] < RES_HEIGHT / 2)
						m_Sprites[i]
								.setScale(0.75f + ((y_Center[i] - y_Min) / (m_Sprites[i]
										.getHeight()* 2)));
					else
						m_Sprites[i]
								.setScale(0.75f + ((y_Max - y_Center[i]) / (m_Sprites[i]
										.getHeight() * 2)));
				}
			}
	}
	
	
	public void onScrollFinished(ScrollDetector pScollDetector, int pPointerID,
			float pDistanceX, float pDistanceY)
	{
		for (int i = 0; i < m_Sprites.length; i++)
		{
			y_Center[i] = (int) (m_Sprites[i].getY() + m_Sprites[i].getHeight() / 2);
			
			if (y_Center[i] >= y_Min + m_Sprites[i].getHeight() / 4
					&& y_Center[i] <= y_Max - m_Sprites[i].getHeight() / 4)
			{
				move(i);
			}
			else if (y_Center[i] >= y_Min
					&& y_Center[i] < y_Min + m_Sprites[i].getHeight() / 4)
			{
				if (m_Focus == i && i < m_Sprites.length)
					move(i + 1);
				else
					move(i);
			}
			else if (y_Center[i] <= y_Max
					&& y_Center[i] > y_Max - m_Sprites[i].getHeight() / 4)
			{
				if (m_Focus == i && i > 0)
					move(i - 1);
				else
					move(i);
			}
		}
	}
	
	private void move(int focus)
	{
		m_Focus = focus;
		
		m_Sprites[focus].registerEntityModifier(new MoveYModifier(DURATION,
				m_Sprites[focus].getY(), y_Start));
		
		m_Sprites[focus].registerEntityModifier(new ScaleModifier(DURATION,
				m_Sprites[focus].getScaleY(), 1));
		
		// sprite sebelah kiri
		for (int j = focus - 1; j >= 0; j--)
		{
			m_Sprites[j].registerEntityModifier(new MoveYModifier(DURATION,
					m_Sprites[j].getY(),(y_Start
					- (m_Sprites[focus].getHeight() + DELTA) * (focus - j))));
			
			m_Sprites[j].registerEntityModifier(new ScaleModifier(DURATION,
					 m_Sprites[j].getScaleY(), 0.75f));
		}
		
		// sprite sebelah kanan
		for (int j = focus + 1; j < m_Sprites.length; j++)
		{
			m_Sprites[j].registerEntityModifier(new MoveYModifier(DURATION,
					m_Sprites[j].getY(), (y_Start + (m_Sprites[focus].getHeight() + DELTA)
						* (j - focus))));
			
			m_Sprites[j].registerEntityModifier(new ScaleModifier(DURATION,
					 m_Sprites[j].getScaleY(), 0.75f));
		}
	}
	
	public int getFocus()
	{
		
		return m_Focus;
	}
	
}
